package com.xiayuanxing.arithmetic.DataStructures.Stack;

import java.util.Scanner;

/**
 * @program: arithmetic
 * @description: 数组模拟栈
 * @author: xia yuan xing
 * @create: 2021-09-07 10:29
 */
public class ArrayStackDemo {

    public static void main(String[] args) {

        //测试
        ArrayStack arrayStack = new ArrayStack(4);
        String key = "";
        boolean loop = true;
        Scanner scanner = new Scanner(System.in);

        while (loop) {
            System.out.println("show: 表示显示栈");
            System.out.println("exit: 退出程序");
            System.out.println("push: 表示添加数据到栈");
            System.out.println("pop: 表示从栈中取出数据");
            System.out.println("请输入您的选择：");

            key = scanner.next();
            switch (key) {
                case "show":
                    arrayStack.List();
                    break;
                case "push":
                    System.out.println("请输入一个数：");
                    int value = scanner.nextInt();
                    arrayStack.push(value);
                    break;
                case "pop":
                    try {
                        int pop = arrayStack.pop();
                        System.out.printf("出栈的数据%d\n", pop);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case "exit":
                    scanner.close();
                    loop = false;
                    break;
            }

            System.out.println("程序退出！");
        }

    }

}

class ArrayStack {

    //栈的大小
    private int maxSize;
    //数组，存放数据
    private int[] stack;
    //top表示栈顶，初始化为-1
    private int top = -1;

    /**
     * 构造器
     *
     * @param maxSize
     */
    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }

    //查看栈顶数据
    public int peek(){
        return stack[top];
    }

    /**
     * 栈是否满
     *
     * @return
     */
    public boolean isFull() {
        return top == maxSize - 1;
    }

    /**
     * 栈是否空
     *
     * @return
     */
    public boolean isEmpty() {
        return top == -1;
    }

    /**
     * 入栈 -push
     *
     * @param value
     */
    public void push(int value) {
        //校验栈是否满
        if (isFull()) {
            System.out.println("栈满了~");
            return;
        }
        top++;
        stack[top] = value;
    }

    /**
     * 出栈-pop
     *
     * @return
     */
    public int pop() {
        //校验栈是否空
        if (isEmpty()) {
            throw new RuntimeException("栈空了~");
        }
        int value = stack[top];
        top--;
        return value;
    }

    /**
     * 遍历栈
     */
    public void List() {
        //校验是否为空
        if (isEmpty()) {
            throw new RuntimeException("栈空了~");
        }

        for (int i = top; i >= 0; i--) {
            System.out.printf("stack[%d]=%d\n", i, stack[i]);
        }
    }


    /**
     * 返回优先级 数字越大，优先级越高
     * @param oper
     * @return
     */
    public int priority(int oper){
        if (oper == '*' || oper == '/'){
            return 1;
        }else if (oper == '+' || oper == '-'){
            return  0;
        }else {
            //目前表达式只适配 + -  * /
            return -1;
        }
    }


    /**
     * 判断是否是一个运算符
     * @param value
     * @return
     */
    public boolean isOper(char value){
        return  value == '+' || value == '-' || value == '*' || value == '/';

    }


    //计算方法
    public int cal(int num1,int num2,int oper){
        //存放运算结果
        int result = 0;
        switch (oper){
            case '+':
                result = num1 + num2;
                break;
            case '-':
                result = num2 - num1;
                break;
            case '*':
                result = num1 * num2;
                break;
            case '/':
                result = num2 / num1;
                break;
        }
        return result;

    }



}